//
//  sureTouZi.m
//  投融保
//
//  Created by wangan on 13-12-19.
//  Copyright (c) 2013年 木叶科技. All rights reserved.
//

#import "SureTouZi.h"
#import "MYWebService.h"
#import "MoneyString.h"

@interface SureTouZi ()

@end

@implementation SureTouZi
@synthesize touZiMoney,touziPassWord,sureTouZiMoney,touZiMoney1,touZiBtn;
extern NSDictionary *touZiDic;
extern NSString *key;
extern NSString *appKey;
extern NSString *proToken;
extern NSString *userRole;//用户角色
int prewTag ;  //编辑上一个UITextField的TAG,
float prewMoveY; //编辑的时候移动的高度

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.view.backgroundColor = [UIColor blackColor];
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    UITapGestureRecognizer *gesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeybard)];
    [self.touZiScroll addGestureRecognizer:gesture];
    self.touziPassWord.secureTextEntry=YES;
    if ([userRole integerValue]==11) {
        [[self.view viewWithTag:100] removeFromSuperview];
        
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)suerBtn:(UIButton *)sender {
    if (touZiMoney.text!=nil&&touZiMoney1.text!=nil&&[touZiMoney1.text isEqualToString:touZiMoney.text]&&touziPassWord.text!=nil) {
        NSString *strAppInvest=[NSString stringWithFormat:@"service=appInvest&amount=%d&password=%@&tradeId=%@&token=%@",[touZiMoney.text integerValue],touziPassWord.text,[touZiDic valueForKey:@"tradeId"],proToken];  //token是“项目详情”返回的
        NSLog(@"-----str---%@",strAppInvest);
        NSLog(@"------str---%@",appKey);
        mbHud = [[MBProgressHUD alloc] initWithView:self.view];
        mbHud.labelText = @"请稍候";
        [self.view addSubview:mbHud];
        mbHud.delegate = self;
        __block NSDictionary *result= [NSDictionary new];
        [mbHud showAnimated:YES whileExecutingBlock:^{
            result =[[MYWebService new] getDataSynWithKey:key encryptedString:strAppInvest appKey:appKey];
        } completionBlock:^{
            sleep(1);
            [mbHud removeFromSuperview];
            if ([[result valueForKey:@"code"]integerValue]==0) {
              
                UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:nil message:[result valueForKey:@"message"] delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil, nil];
                [myAlert show];
            }else{
                NSString *message=[result valueForKey:@"message"];
                NSString *cancel=@"确定";
                ALERTVIEW(message, cancel);
                self.sureTouZiMoney.text=@"";
                self.touZiMoney.text=@"";
                self.touziPassWord.text=@"";
                self.touZiMoney1.text=@"";
            }
            
        }];
        
        
    }else if(touZiMoney.text==nil||touZiMoney1.text==nil){
        
        UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:@"投资金额不能为空" message:@"请输入投资金额" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [myAlert show];
    }else if(touziPassWord.text==nil){
        UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:@"您好" message:@"请输入支付密码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [myAlert show];
        
    }else if (![touZiMoney1.text isEqualToString:touZiMoney.text]){
        UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:@"您好" message:@"两次投资金额不一致，请重新输入" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [myAlert show];
    }
    
}

- (IBAction)backBtn:(UIButton *)sender {
//    NSString *strAppInvest=@"service=appInvest&amount=20000&password=123qwe&tradeId=467&token=33032e6e-9bfc-4902-9bc9-1b8a68577669";  //token是“项目详情”返回的token
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)textExit:(id)sender {
    [self.touZiScroll setContentOffset:CGPointMake(0, 0) animated:YES];
    [self.view endEditing:YES];
}

#pragma mark +++++++++键盘+++++++++++
/**
 开始编辑UITextField的方法
 */
-(void) textFieldDidBeginEditing:(UITextField *)textField
{
    
    CGRect textFrame =  textField.frame;
    float textY = textFrame.origin.y+textFrame.size.height;
    float bottomY = self.touZiScroll.frame.size.height-textY;
    if(bottomY>=260)  //判断当前的高度是否已经有216，如果超过了就不需要再移动主界面的View高度
    {
        prewTag = -1;
        return;
    }
    prewTag = textField.tag;
    float moveY;
    if (SCREEN_HEIGHT>480) {
        moveY = 260-bottomY;
    }else
    {
        moveY = 300-bottomY;
    }
    
    prewMoveY = moveY;
    [UIView animateWithDuration:0.25 animations:^{
        [self.touZiScroll setContentOffset:CGPointMake(0, prewMoveY) animated:YES];
    }];
    
}

/**
 结束编辑UITextField的方法，让原来的界面还原高度
 */
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    //设置大写金额
    if (textField==self.touZiMoney) {
        self.sureTouZiMoney.text=[MoneyString moneyStringWithDigitStr:self.touZiMoney.text];
    }

    if(prewTag == -1) //当编辑的View不是需要移动的View
    {
        return;
    }
    float moveY ;
    
    if(prewTag == textField.tag) //当结束编辑的View的TAG是上次的就移动
    {   //还原界面
        moveY =  prewMoveY;
        [self.touZiScroll setContentOffset:CGPointMake(0, moveY)];
    }

    
}
#pragma mark


- (void)closeKeybard
{
    [self.touZiScroll setContentOffset:CGPointMake(0, 0) animated:YES];
    [self.view endEditing:YES];
}

#pragma mark-   -----------状态栏底色------
- (UIStatusBarStyle)preferredStatusBarStyle
{
    [self.view setBackgroundColor:[UIColor blackColor]];
    return UIStatusBarStyleLightContent;
}
- (BOOL)prefersStatusBarHidden
{
    return NO;
}
@end
